import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/util/client.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_app/model/ProductModel.dart';
import 'package:flutter_app/util/toast.dart';

class ProductWidget extends StatefulWidget {
  final String id;

  ProductWidget({Key key, this.id}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _ProductWidgetState();
  }
}

class _ProductWidgetState extends State<ProductWidget> {
  ProductModelData _product;

  int _bottomNavigationBarIndex = 0;

  getData() {
    String id = widget.id;
    //print("id " + id);
    Client.post("http://wxapp.qqvbc.com/crab/v2/getitem",
        params: {"num_iid": id}).then((jsonStr) {
      print(jsonStr);
      ProductModel p = new ProductModel(jsonStr);
      setState(() {
        _product = p.data;
      });
    });
  }

  @override
  void initState() {
    print("init state");
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (_product == null) {
      return new Container(child: new Center(child: new Text("加载中...")));
    }

    return new Scaffold(
      appBar: new AppBar(
        title: new Text('详情'),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            color: Colors.white,
            onPressed: () {
              Navigator.of(context).pushNamed('/new/xxx');
            },
          )
        ],
      ),
      body: new Column(
        children: <Widget>[
          _swiper(context),
          new Container(
            padding: EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                new Text(_product.title),
                new Stack(
                  children: [
                    new Text("天猫价 ¥" + _product.zk_price.toString(),
                        style: new TextStyle(color: const Color(0xaaaaaaaa))),
                    new Align(
                      alignment: FractionalOffset.centerRight,
                      child: new Text("已售 " + _product.volume.toString() + "件",
                          style: new TextStyle(color: const Color(0xaaaaaaaa))),
                    )
                  ],
                ),
                new Stack(
                  children: [
                    new Text("劵后价 ¥" + _product.qh_price.toString(),
                        style: new TextStyle(color: const Color(0xffff0000))),
                    new Align(
                      alignment: FractionalOffset.centerRight,
                      child: new Text(
                          "¥" + _product.coupon_money.toString() + "元劵",
                          style: new TextStyle(color: const Color(0xffff0000))),
                    ),
                  ],
                ),
              ],
            ),
          )
        ],
      ),
      bottomNavigationBar: new BottomNavigationBar(
        onTap: (int index) {
          _bottomNavigationBarOnTap(context, index);
        },
        currentIndex: _bottomNavigationBarIndex,
        items: [
          new BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text('Home'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.person),
            title: new Text('Profile'),
          ),
        ],
      ),
    );
  }

  void _bottomNavigationBarOnTap(BuildContext context, int index) {
    _copyCode(context, "123");

    setState(() {
      _bottomNavigationBarIndex = index;
    });
  }

  Widget _swiper(BuildContext context) {
    return new Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.width,
        child: Swiper(
          itemBuilder: _swipeBuilder,
          itemCount: _product == null ? 0 : _product.small_images.length,
          pagination: new SwiperPagination(
              builder: DotSwiperPaginationBuilder(
            color: Colors.black54,
            activeColor: Colors.white,
          )),
          control: new SwiperControl(),
          scrollDirection: Axis.horizontal,
          autoplay: true,
          onTap: (index) => print('点击了第$index个'),
        ));
  }

  Widget _swipeBuilder(BuildContext context, int index) {
    return Image.network(
      _product.small_images[index],
      fit: BoxFit.fill,
    );
  }

  _copyCode(BuildContext context, String text) {
    if (text == null) return;
    Clipboard.setData(new ClipboardData(text: text));
    print("coupon code:");
    print(text);
    Toast.show(context, " 已经复制");
  }
}
